/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProductPnl.java
 *
 * Created on Jul 17, 2011, 10:31:43 AM
 */
package com.viweb.storemanager.ui.product;

import com.viweb.storemanager.core.ColumnData;
import com.viweb.storemanager.core.CursorManager;
import com.viweb.storemanager.core.DefaultObjectTableModel;
import com.viweb.storemanager.core.OptionPane;
import com.viweb.storemanager.dao.Biz;
import com.viweb.storemanager.entity.Product;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;

/**
 *
 * @author qhvic
 */
public class ProductPnl extends javax.swing.JPanel {

    private JTable headerTable;
    private ArrayList<Product> products;
    private ProductAddEditPnl addEditPanel;

    /** Creates new form ProductPnl */
    public ProductPnl() {
        this.products = new ArrayList<Product>();
        initProductsList();
        initComponents();
        addEditPanel = new ProductAddEditPnl(this);
    }

    private void initProductsList() {
        this.products.clear();
        this.products = Biz.getInstance().selectAllProduct();
    }

    public ProductAddEditPnl getCreationPanel() {
        return this.addEditPanel;
    }

    private void tblListSelectionValueChanged(javax.swing.event.ListSelectionEvent evt) {
        if (!evt.getValueIsAdjusting()) {
            if (this.tblList.getSelectedRow() >= 0) {
                Product product = this.products.get(this.tblList.getSelectedRow());
                this.addEditPanel.assignProduct(product);
                this.addEditPanel.setEditableMode(false);
            }
        }
    }

    public void updateComponentsUI(Product product) {
        this.initProductsList();
        int rowSelection = this.products.indexOf(product);
        this.tblList.getSelectionModel().setSelectionInterval(rowSelection, rowSelection);
        this.headerTable.updateUI();
        this.tblList.updateUI();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        scpList = new javax.swing.JScrollPane();
        tblList = new javax.swing.JTable();
        btnAdd = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();

        setLayout(new java.awt.GridBagLayout());

        scpList.setPreferredSize(new java.awt.Dimension(452, 402));

        tblList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tblList.setRowHeight(22);
        ColumnData[] tableColumns = {
            new ColumnData("Mã hàng", 100, SwingConstants.LEFT, 1),
            new ColumnData("Tên hàng", 100, SwingConstants.LEFT, 2),
            new ColumnData("Loại hàng", 100, SwingConstants.LEFT, 7),
            new ColumnData("Kho hàng", 100, SwingConstants.LEFT, 4),
            new ColumnData("Số lượng", 100, SwingConstants.LEFT, 3),
            new ColumnData("Đơn giá", 100, SwingConstants.LEFT, 5),
            new ColumnData("Đơn vị tính", 100, SwingConstants.LEFT, 6),
            new ColumnData("Tình trạng", 100, SwingConstants.LEFT, 9),
        };
        DefaultObjectTableModel tableModel = new DefaultObjectTableModel(tblList, tableColumns, products, false);

        headerTable = tableModel.getHeaderTable();
        headerTable.createDefaultColumnsFromModel();
        tblList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        tblList.getSelectionModel().addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                tblListSelectionValueChanged(evt);
            }
        });

        // Put it in a viewport that we can control a bit
        JViewport viewport = new JViewport();
        viewport.setView(headerTable);
        viewport.setPreferredSize(headerTable.getMaximumSize());

        scpList.setRowHeader(viewport);
        scpList.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, headerTable
            .getTableHeader());
        tblList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblListMouseClicked(evt);
            }
        });
        scpList.setViewportView(tblList);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(scpList, gridBagConstraints);

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/viweb/storemanager/imgae/add.png"))); // NOI18N
        btnAdd.setText("Thêm");
        btnAdd.setToolTipText("Thêm mới nhân viên");
        btnAdd.setMaximumSize(new java.awt.Dimension(80, 26));
        btnAdd.setMinimumSize(new java.awt.Dimension(80, 26));
        btnAdd.setPreferredSize(new java.awt.Dimension(80, 26));
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 5);
        add(btnAdd, gridBagConstraints);

        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/viweb/storemanager/imgae/edit.png"))); // NOI18N
        btnEdit.setText("Sửa");
        btnEdit.setToolTipText("Hiệu chỉnh thông tin nhân viên");
        btnEdit.setMaximumSize(new java.awt.Dimension(80, 26));
        btnEdit.setMinimumSize(new java.awt.Dimension(80, 26));
        btnEdit.setPreferredSize(new java.awt.Dimension(80, 26));
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 5);
        add(btnEdit, gridBagConstraints);

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/viweb/storemanager/imgae/remove.png"))); // NOI18N
        btnDelete.setText("Xóa");
        btnDelete.setToolTipText("Xóa nhân viên");
        btnDelete.setMaximumSize(new java.awt.Dimension(80, 26));
        btnDelete.setMinimumSize(new java.awt.Dimension(80, 26));
        btnDelete.setPreferredSize(new java.awt.Dimension(80, 26));
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 5);
        add(btnDelete, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        if (this.tblList.getSelectedRow() != -1) {
            this.addEditPanel.setEditableMode(true);
        } else {
            OptionPane.show(JOptionPane.getFrameForComponent(ProductPnl.this),
                    "Chưa chọn thông tin cần sửa đổi!", "Thông báo");
        }
    }//GEN-LAST:event_btnEditActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        Product product = new Product();
        this.addEditPanel.assignProduct(product);
        this.addEditPanel.setEditableMode(true);
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        if (this.tblList.getSelectedRow() != -1) {
            CursorManager.setWaitCursor(this);

            if (OptionPane.show(JOptionPane.getFrameForComponent(ProductPnl.this),
                    "Khách hàng và tất cả các thông tin liên quan sẽ bị xoá!\n"
                    + "Có chắc chắn thực hiện thao tác này không?",
                    "Thông báo",
                    OptionPane.MsgTYPE_YESNO) == OptionPane.MsgRESULT_YES) {

                Product product = this.products.get(this.tblList.getSelectedRow());

                if (!Biz.getInstance().deleteProduct(product.getId())) {
                    OptionPane.show(JOptionPane.getFrameForComponent(ProductPnl.this),
                            Biz.getInstance().getLastError(),
                            "Thông báo");
                } else {
                    int rowSelection = this.products.indexOf(product);
                    if (rowSelection == this.products.size() - 1) {
                        this.products.remove(product);
                        if (rowSelection != 0) {
                            this.tblList.getSelectionModel().setSelectionInterval(rowSelection - 1,
                                    rowSelection - 1);
                        } else {
                            this.tblList.getSelectionModel().clearSelection();
                        }
                    } else {
                        this.products.remove(product);
                    }

                    this.headerTable.updateUI();
                    this.tblList.updateUI();
                }

                if (this.tblList.getSelectedRow() >= 0) {
                    Product currentEmployee = this.products.get(this.tblList.getSelectedRow());
                    this.addEditPanel.assignProduct(currentEmployee);
                } else {
                    this.addEditPanel.assignProduct(null);
                }
            }

            CursorManager.setDefaultCursor(this);
        } else {
            OptionPane.show(JOptionPane.getFrameForComponent(ProductPnl.this),
                    "Chưa chọn thông tin cần xoá!",
                    "Thông báo");
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void tblListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblListMouseClicked
        // TODO add your handling code here:
        if (evt.getClickCount() == 2) {
            if (this.tblList.getSelectedRow() != -1) {
                this.addEditPanel.setEditableMode(true);
            } else {
                OptionPane.show(JOptionPane.getFrameForComponent(ProductPnl.this),
                        "Chưa chọn thông tin cần sửa đổi!", "Thông báo");
            }
        }
    }//GEN-LAST:event_tblListMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JScrollPane scpList;
    private javax.swing.JTable tblList;
    // End of variables declaration//GEN-END:variables
}
